package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.CaseInsensitiveMap;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtProductActivityStock;
import com.ymttest.datadriver.SqlSearch;
import com.ymttest.utils.MapUtil;

public class YmtProductActivityStockWapper implements SqlSearch{
	SqlManager manage;

	public YmtProductActivityStockWapper() {
		manage = new SqlManager("intpro");
	}
	
	
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	/**
	 * 返回sqlmanager
	 * 
	 * @return
	 */
	public SqlManager getManager() {
		return manage;
	}

	
	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}
	
	
	/***
	 * 根据商品Id 和活动Id 修改活动库存
	 * @param productId
	 * @param activityId
	 * @param stockNum
	 * @return
	 */
	public int updateStockByActivityIdAndProductId(String productId, int activityId, int stockNum){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("sProductID", productId);
		map.put("iActivityID", activityId);
		map.put("iStockNum", stockNum);
		return updateStockByActivityIdAndProductId(map);
	}
	
	
	public int updateStartTimeByProductId(String productId, String beginTime){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("sProductID", productId);
		map.put("dBeginTime", beginTime);
		int i = manage.update("com.ymttest.database.mapping.YmtProductActivityStockMapper.updateStartTimeByProductId", map);
		manage.commit();
		// manage.close();
		return i;
	}
	
	/**
	 * 根据iProductInActivityId修改beginTime
	 * @param iProductInActivityId
	 * @param beginTime
	 * @return
	 */
	public int updateStartTimeByiProductInActivityId(Integer iProductInActivityId, String beginTime){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("iProductInActivityId", iProductInActivityId);
		map.put("dBeginTime", beginTime);
		int i = manage.update("com.ymttest.database.mapping.YmtProductActivityStockMapper.updateStartTimeByiProductInActivityId", map);
		manage.commit();
		// manage.close();
		return i;
	}
	public int updateStockByActivityIdAndProductId(Map<String, Object> record) {
		int i = manage.update("com.ymttest.database.mapping.YmtProductActivityStockMapper.updateStockByActivityIdAndProductId", record);
		manage.commit();
		// manage.close();
		return i;
	}
	
	/**
	 * 把商品的活动库存改成0
	 * @param pid
	 * @return
	 */
	public int updateStock0ByProductId(String pid) {
		int i = manage.update("com.ymttest.database.mapping.YmtProductActivityStockMapper.updateStock0ByProductId", pid);
		manage.commit();
		return i;
	}
	
	public List<YmtProductActivityStock> selectByProductId(String productId)throws InterruptedException{
		  @SuppressWarnings("unchecked")
		List<YmtProductActivityStock>  ymtProductActivityStockList = manage.getSelectResults(
		            "com.ymttest.database.mapping.YmtProductActivityStockMapper.selectByProductId", productId);
		    manage.commit();
		    return ymtProductActivityStockList;
	}
	
	public YmtProductActivityStock selectByCatalogId(String catalogId)throws InterruptedException{
		  @SuppressWarnings("unchecked")
		List<YmtProductActivityStock>  ymtProductActivityStockList = manage.getSelectResults(
		            "com.ymttest.database.mapping.YmtProductActivityStockMapper.selectByCatalogId", catalogId);
		    manage.commit();
		    if (ymtProductActivityStockList.size()==0){
		    	return null;
		    }else{
		    	return ymtProductActivityStockList.get(0);
		    }
	}
	
	public int updateiStockNumByProductActivityStockId(String productActivityStockID,int stockNum) {
		
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("productActivityStockID", productActivityStockID);
		map.put("stockNum", stockNum);
		
		int i = manage.update("com.ymttest.database.mapping.YmtProductActivityStockMapper.updateiStockNumByProductActivityStockId", map);
		manage.commit();
		// manage.close();
		return i;
	}
	
	
	public int updateSourceIdByProductActivityStockId(String productActivityStockID,int sourceId) {
		
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("productActivityStockID", productActivityStockID);
		map.put("sourceId", sourceId);
		
		int i = manage.update("com.ymttest.database.mapping.YmtProductActivityStockMapper.updateSourceIdByProductActivityStockId", map);
		manage.commit();
		// manage.close();
		return i;
	}
	/**
	 * 根据productInActivityId 查询活动库存信息
	 * 这个数据可能会延时进入数据库所以没查到会等一下再查
	 * @param productInActivityId
	 * @return
	 * @throws InterruptedException
	 */
	public YmtProductActivityStock selectByProductInActivityId(int productInActivityId) throws InterruptedException{
		List<YmtProductActivityStock>  ymtProductActivityStockList = manage.getSelectResults(
	            "com.ymttest.database.mapping.YmtProductActivityStockMapper.selectByProductInActivityId", productInActivityId);
	    manage.commit();
	    if(ymtProductActivityStockList==null||ymtProductActivityStockList.isEmpty()){
	    	Thread.sleep(5000);
	    	 ymtProductActivityStockList = manage.getSelectResults(
	 	            "com.ymttest.database.mapping.YmtProductActivityStockMapper.selectByProductInActivityId", productInActivityId);
	 	    manage.commit();
	    }
	    return ymtProductActivityStockList.get(0);
	}
	
	public List<YmtProductActivityStock> selectInActivityId(int productInActivityId){
		List<YmtProductActivityStock>  ymtProductActivityStockList = manage.getSelectResults(
	            "com.ymttest.database.mapping.YmtProductActivityStockMapper.selectByProductInActivityId", productInActivityId);
	    manage.commit();
	    return ymtProductActivityStockList;
	}
	
	
	public Map selectBy(Map<String, Object> searchMap){
		  @SuppressWarnings("unchecked")
		List<Map>  sProductIdlist = manage.getSelectResults(
		            "com.ymttest.database.mapping.YmtProductActivityStockMapper.selectBy", searchMap);		    
		  manage.commit();
		  return  new CaseInsensitiveMap(sProductIdlist.get(0));
	}
	
	public List<Map> selectAllBy(Map<String, Object> searchMap){
		  List<Map>  sProductIdlist = manage.getSelectResults(
		            "com.ymttest.database.mapping.YmtProductActivityStockMapper.selectBy", searchMap);		    
		  manage.commit();
		  return  sProductIdlist;
	}
	
	
	
	public int updateiActionByActivityIdAndProductId(int iAction,String sProductID,Integer iActivityID ) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("iAction", iAction);
		map.put("sProductID", sProductID);
		map.put("iActivityID", iActivityID);
		
		int i = manage.update("com.ymttest.database.mapping.YmtProductActivityStockMapper.updateiActionByActivityIdAndProductId", map);
		manage.commit();
		return i;
	}
	
	public static void main(String[] args) throws InterruptedException {
		System.out.println(new YmtProductActivityStockWapper().updateStartTimeByProductId("719c3a2f-0e39-4a4d-9571-dbb72e39eae7", "2016-09-28 17:27:39.000"));
	}
}
